package com.rzico.sbl.ui.statistic

import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.utils.observe
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityStockSupplyBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.StockSupply
import com.rzico.sbl.viewmodel.StatisticStockViewModel
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.conditionFalse
import com.xinnuo.common.extend.formatDecimal
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.orEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.bottomHide
import com.xinnuo.common.helper.bottomShow
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.addOnScrollChanged
import com.xinnuo.common_ui.utils.showHintDialog
import com.xinnuo.slimadapter.SlimAdapter

class StockSupplyActivity : BaseActivity(R.layout.activity_stock_supply) {

    private val mBinding by lazy { ActivityStockSupplyBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticStockViewModel>()

    override fun initData() {
        initTitle(title = "供应商")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout
                    .emptyHint
                    .text = "暂无相关供应商信息！"

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(bottom = 60f))
                    layout.addOnScrollChanged(
                        onShow = { mBinding.supplyBottom.bottomShow() },
                        onHide = { mBinding.supplyBottom.bottomHide(55f) }
                    )

                    mAdapter = SlimAdapter.creator()
                        .register<StockSupply>(R.layout.item_stock_supply) { jector, bean, index ->
                            jector.apply {
                                text(R.id.item_supply_name, bean.name)
                                text(R.id.item_supply_code, bean.code.orEmpty())
                                text(R.id.item_supply_url, bean.url.orEmpty())
                                text(R.id.item_supply_time, bean.createDate.getTimeString())
                                text(R.id.item_supply_money, bean.arrears.formatDecimal())
                                text(R.id.item_supply_barrel, bean.arrearsQuantity.orEmpty("0"))

                                clicked {
                                    startActivityEx<StockSupplyModifyActivity>(
                                        "title" to "供应商编辑",
                                        "mode" to "edit",
                                        "bean" to bean
                                    )
                                }

                                longClicked {
                                    deleteSupply(bean.id, index)
                                    true
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    override fun initListener() {
        mBinding
            .btAdd
            .oneClick {
                startActivityEx<StockSupplyModifyActivity>(
                    "title" to "添加供应商",
                    "mode" to "add"
                )
            }

        observe<String>("supplyList") { updateList() }
    }

    /**
     * 删除供应商
     */
    private fun deleteSupply(itemId: String, index: Int) {
        showHintDialog(
            hint = "确定要删除供应商(含欠款和欠桶)吗？",
            sure = "删除",
            onSure = {
                getViewModel()
                    .request(
                        requestUrl = BaseUrl.supDelete,
                        params = arrayOf("ids" to itemId),
                        onSuccess = {
                            showToast("删除成功！")

                            mList.removeAt(index)
                            mAdapter.apply {
                                notifyItemRemoved(index)
                                notifyItemRangeChanged(index, mList.size - index)
                            }

                            mBinding.listLayout
                                .emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                    )
            }
        )
    }

    /**
     * 供应商列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .supplyList(
                page = index,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it.data)
                        it.data.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}